clear
set mem 1g
set more off
prog drop _all
capture log close

capture log close 

global dir_fig_out     "../outputs"
global dir_data_in     "out_files"
global dir_work_out    "out_figs"
global dir_smm_in    "../nlsy97/out_datasmm_new"


pause off

	local graph1_options "ms(T) mc(black) mfc(black) c(l) lp(shortdash) lc(black)  lwidth(1.2)"
	local graph2_options "ms(O) mc(green) mfc(green) c(l) lp(dash_dot) lc(green)   lwidth(1.2)"
	local graph3_options "ms(T) mc(blue) mfc(none) c(l)   lp(longdash) lc(blue)   lwidth(1.2)"
	local graph4_options "ms(D) mc(red) mfc(red) c(l)     lp(solid) lc(red)   lwidth(1.2)"
	local graph5_options "ms(D) mc(red) mfc(none) c(l) lp(dash) lc(red)"
	local graph6_options "ms(O) mc(green) mfc(none) c(l) lp(dash) lc(green)"


set scheme s1color



******************** Non-targeted moments: smoking gap  
** Figure 4(a): difference in smoking rate

use $dir_data_in/sim_model_part1, clear

capture matrix drop reg_out

local minage 18 
local maxage 24

foreach age of numlist `minage'/`maxage' {

	reg dq enroll if addiction == 0 & age == `age' 
	
matrix dq0_b    = get(_b)'
matrix dq0_bcov = get(VCE) 
matrix dq0_bvar = vecdiag(dq0_bcov)' 

matmap dq0_bvar dq0_bse, map(sqrt(@))

matrix list dq0_b 
matrix list dq0_bse 

matrix reg_out      = [ nullmat(reg_out)   \ `age', dq0_b[1,1], dq0_bse[1,1] ]  

} 

matrix list reg_out


clear
svmat reg_out

rename reg_out1 age
rename reg_out2 model_coef_dq0

gen data = 2

label define ldata 1 "NLSY data" 2 "Benchmark model", replace
label values data ldata 


merge 1:1 age using $dir_smm_in/nontargeted_gap_dq0_byAge.dta, keepusing(coef_* se_*) 
assert _merge == 3
drop _merge

generate hi = coef  + 1.96 * se
generate lo = coef  - 1.96 * se

list 

local label1 "NLSY Data"
local label2 "Benchmark Model"

label var age "Age"

local xvar age 

	local graph0_options "ms(i) c(l) lp(dot) lc(blue)"
	local graph1_options "ms(i) mc(blue) mfc(blue) c(l) lp(dash) lc(blue)"
*	local graph1_options "ms(T) mc(blue) mfc(blue) c(l) lp(dash) lc(blue)"
	local graph4_options "ms(T) mc(blue) mfc(none) c(l) lp(solid) lc(blue)"
	
	local graph2_options "ms(D) mc(red) mfc(red) c(l) lp(solid) lc(red)"
	local graph3_options "ms(O) mc(green) mfc(green) c(l) lp(solid) lc(green)"
	local graph5_options "ms(D) mc(red) mfc(none) c(l) lp(dash) lc(red)"
	local graph6_options "ms(O) mc(green) mfc(none) c(l) lp(dash) lc(green)"
	
		sum `xvar'
		local xmin = `r(min)'
		local xmax = `r(max)'
		local xsup = `xmax'+1 
		local xinf = `xmin'-1 		
	
		local step = 1 + 2*(`xmax'-`xmin'>20) 

	
	twoway rarea lo hi `xvar' , color(gs10) || scatter coef `xvar', `graph1_options' || scatter model_coef `xvar', `graph4_options' legend(lab(1 "95% CI" ) lab(2 `label1') lab(3 `label2')  ring(1)) ///
	ytitle(" Difference in smoking initiation ") legend(on region(lstyle(none))) scheme(s1color) ///
	xlabel(`xmin'(`step')`xmax', valuelabel)  xscale( range(`xinf', `xsup')) ///
	ylabel(-0.15(0.05)0.05, grid)  

	graph export out_figs/fit_nontargeted_gap_dq0_byAge.pdf, as(pdf) replace 
	graph export out_figs/fit_nontargeted_gap_dq0_byAge.eps, as(eps) replace 


	
graph export $dir_fig_out/Figure_04a.pdf, as(pdf) replace 
graph export $dir_fig_out/Figure_04a.eps, as(eps) replace 




******************** Non-targeted moments: smoking relapse at age 30
** Figure 4(b): smoking relapse at age 30 by the number of years smoked 

use $dir_data_in/sim_model_part1, clear

xtset id age 

gen dq_a30_ = dq if age == 30
by id: egen dq_a30 = max(dq_a30_)
drop dq_a30_

gen addiction_cat = .
replace addiction_cat = 1 if addiction == 1
replace addiction_cat = 2 if addiction == 2 | addiction == 3
replace addiction_cat = 3 if addiction == 4 | addiction == 5
replace addiction_cat = 4 if addiction > 5 & addiction != . 

	tabstat dq_a30 if dq == 0 & age <= 29 & age >= 25, by(addiction_cat) stat(mean) columns(statistics) save	
	matrix vec_mean_c = [ r(Stat1), r(Stat2), r(Stat3), r(Stat4)]
	
	matrix vec = [vec_mean_c']
	
	matrix list vec 
	
	clear
	
	svmat vec
	rename vec1     model_dq_add

	gen data = 2
		
	gen addcat = _n 
	
label var addcat "Years smoked"
label define laddcat 1 "1 year" 2 "2 or 3 years" 3 "4 or 5 years" 4 "> 5 years", replace
label values addcat laddcat 


merge 1:1 addcat using $dir_smm_in/nontargeted_smokingrelapse.dta 
assert _merge == 3
drop _merge

generate hi = dq_add  + 1.96 * se
generate lo = dq_add  - 1.96 * se


label define ldata 1 "NLSY data" 2 "Benchmark model", replace
label values data ldata 

list 

local label1 "NLSY Data"
local label2 "Benchmark Model"


label define ldata 1 "NLSY data" 2 "Benchmark model", replace
label values data ldata 


local xvar addcat


	local graph0_options "ms(i) c(l) lp(dot) lc(blue)"
	local graph1_options "ms(i) mc(blue) mfc(blue) c(l) lp(dash) lc(blue)"
*	local graph1_options "ms(T) mc(blue) mfc(blue) c(l) lp(dash) lc(blue)"
	local graph4_options "ms(T) mc(blue) mfc(none) c(l) lp(solid) lc(blue)"
	
	local graph2_options "ms(D) mc(red) mfc(red) c(l) lp(solid) lc(red)"
	local graph3_options "ms(O) mc(green) mfc(green) c(l) lp(solid) lc(green)"
	local graph5_options "ms(D) mc(red) mfc(none) c(l) lp(dash) lc(red)"
	local graph6_options "ms(O) mc(green) mfc(none) c(l) lp(dash) lc(green)"
	
		sum `xvar'
		local xmin = `r(min)'
		local xmax = `r(max)'
		local xsup = `xmax'+1 
		local xinf = `xmin'-1 		
	
		local step = 1 + 2*(`xmax'-`xmin'>20) 

	
	twoway rarea lo hi `xvar' , color(gs10) || scatter dq_add `xvar', `graph1_options' || scatter model_dq_add `xvar', `graph4_options' legend(lab(1 "95% CI" ) lab(2 `label1') lab(3 `label2')  ring(1)) ///
	ytitle("Smoking relapse at age 30") legend(on region(lstyle(none))) scheme(s1color) ///
	xlabel(`xmin'(`step')`xmax', valuelabel)  xscale( range(`xinf', `xsup')) ///
	ylabel(0.0(0.15)0.6, grid)  
	
	graph export out_figs/fit_nontargeted_smokingrelapse.pdf, as(pdf) replace 
	graph export out_figs/fit_nontargeted_smokingrelapse.eps, as(eps) replace 

graph export $dir_fig_out/Figure_04b.pdf, as(pdf) replace 
graph export $dir_fig_out/Figure_04b.eps, as(eps) replace 


capture log close 


